<?php
// 添加文章

require_once 'template/background/contents.php';
require_once '../api/engine/MarkdownRenderer.php';

function generateRandomString($length = 10) {
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function saveArticle($articleTitle, $articleContents){
    $timeStamp = time();
    // 检测标题是否包含中文字符
    if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $articleTitle)) {
        // 如果包含中文，生成随机英文字符串加上时间戳作为文件名
        $randomString = generateRandomString();
        $ArticleTitle = $timeStamp . "_" . $randomString;
    } else {
        // 否则，直接使用标题加上时间戳
        $ArticleTitle = $timeStamp . "_" . $articleTitle;
    }

    $filePath = "template/articles_save/" . $ArticleTitle . ".html";

    $directory = dirname($filePath);

    // 检查目录是否存在，如果不存在则创建它
    if (!is_dir($directory)) {
        mkdir($directory, 0777, true);
    }

    // 存储到本地
    // 将原标题（不带有时间戳）写入进去
    $htmlContent = "
    <!DOCTYPE html>
    <html lang='zh-CN'>
    <head>
        <meta charset='UTF-8'>
        <title>$articleTitle</title>
        <style>
            ". BLOG_PAGE_STYLE ."
        </style>
    </head>
    <body>
        <div class='header'>
            <h1>$articleTitle</h1>
        </div>
        <div class='content'>
            $articleContents
        </div>
      
    </body>
    </html>
    ";

    file_put_contents($filePath, $htmlContent);
}

/**
 * 添加文章
 * @return array|false
 */
function addArticle(){
    // 处理表单提交
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $articleTitle = $_POST['article_title'];
        $articleContent = $_POST['article_content'];

        // 使用 MarkdownRenderer 处理内容
        $markdownRenderer = new \PolarisbackEndFrameRe\api\engine\MarkdownRenderer();
        $renderedContent = $markdownRenderer->parse($articleContent);

        saveArticle($articleTitle, $renderedContent);
    }
    return false;
}

addArticle();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加文章</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        textarea {
            width: 100%;
            height: 200px;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            font-size: 16px;
            color: white;
            background-color: #007BFF;
            border: none;
            border-radius: 5px;
            text-decoration: none;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<h1>添加文章 Support MarkDown</h1>
<form method="post" action="">
    <div class="form-group">
        <label for="article_title">文章标题:</label>
        <input type="text" id="article_title" name="article_title" required>
    </div>
    <div class="form-group">
        <label for="article_content">文章内容:</label>
        <textarea id="article_content" name="article_content" required></textarea>
    </div>
    <button type="submit" class="btn">提交</button>
</form>
</body>
</html>
